import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xiaozhoubg/pages/tabs/bookpage.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import '../../service/service_method.dart';
import '../../res/home_data.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<Widget> imageList = List();
  // var bookList;
  List bookList = [];
  void initState() {
    imageList.add(Image.network(
      'http://www.xiaozhoubg.com/public/uploads/IMG/carouselImg/1582945843621.jpg',
      fit: BoxFit.fill,
    ));
    imageList.add(Image.network(
      'http://www.xiaozhoubg.com/public/uploads/IMG/carouselImg/1582945214490.jpg',
      fit: BoxFit.fill,
    ));
    imageList.add(Image.network(
      'http://www.xiaozhoubg.com/public/uploads/IMG/carouselImg/1580994399873.jpg',
      fit: BoxFit.fill,
    ));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
      context,
      width: 750,
      height: 1334,
    );
    return SingleChildScrollView(
      padding: EdgeInsets.fromLTRB(20, 50, 20, 0),
      child: Column(
        children: <Widget>[
          //轮播图

          Container(
            width: ScreenUtil().setWidth(672),
            height: ScreenUtil().setHeight(264),
            child: Swiper(
              itemBuilder: _swiperBuilder,
              itemCount: 3,
              pagination: SwiperPagination(), // 控制分页指示器（下面的小圆点）
              autoplay: true, //自动循环播放
              duration: 1000, //动画时间
              onTap: (int index) {
                print("点击了${index}个图片");
              },
            ),
          ),
          SizedBox(height: 30),
          // 精选手册
          Container(
            // height: ScreenUtil().setHeight(30),
            child: Row(
              children: <Widget>[
                Container(
                  child: Text(
                    "精选手册",
                    style: TextStyle(fontSize: 20, color: Colors.black),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: 200),
                  child: InkWell(
                    child: Text(
                      "查看更多  >",
                      style: TextStyle(
                        color: Color.fromRGBO(179, 179, 179, 1),
                      ),
                    ),
                    onTap: () {
                      Navigator.pushNamed(context, '/bookpage');
                    },
                  ),
                )
              ],
            ),
          ),
          // SizedBox(height: 20),
          //手册内容
          Container(
            height: ScreenUtil().setHeight(550),
            child: ListView.builder(
              itemCount: bookData.length,
              itemBuilder: (context, index) {
                return Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Container(
                          width: ScreenUtil().setWidth(200),
                          height: ScreenUtil().setHeight(125),
                          child: Image.network(bookData[index]["imageUrl"]),
                        ),
                        SizedBox(width: 25),
                        Container(
                          height: ScreenUtil().setHeight(125),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Text(
                                "${bookData[index]["title"]}",
                                style: TextStyle(
                                  fontSize: 18,
                                ),
                              ),
                              SizedBox(
                                height: 15,
                              ),
                              Text(
                                "${bookData[index]["time"]}",
                                style: TextStyle(
                                    color: Color.fromRGBO(179, 179, 179, 1),
                                    fontSize: 12),
                              )
                            ],
                          ),
                        ),
                        Container(
                          height: 20,
                          width: 60,
                          margin: EdgeInsets.fromLTRB(75, 0, 0, 0),
                          child: RaisedButton(
                            padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                            textColor: Colors.white,
                            child: Text(
                              "进入",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 12,
                              ),
                            ),
                            onPressed: () {
                              //路由跳转
                              Navigator.of(context).push(MaterialPageRoute(
                                  builder: (context) => BookPage()));
                            },
                            color: Colors.blue,
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(15)),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: 18)
                  ],
                );
              },
            ),
          ),
          SizedBox(height: 20),
          //精选博客
          Container(
            // height: ScreenUtil().setHeight(30),
            child: Row(
              children: <Widget>[
                Container(
                  child: Text(
                    "精选博客",
                    style: TextStyle(fontSize: 20, color: Colors.black),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: 200),
                  child: InkWell(
                    child: Text(
                      "查看更多  >",
                      style: TextStyle(
                        color: Color.fromRGBO(179, 179, 179, 1),
                      ),
                    ),
                    onTap: () {
                      Navigator.pushNamed(context, '/bookpage');
                    },
                  ),
                )
              ],
            ),
          ),
          //博客内容
          Container(
            height: ScreenUtil().setHeight(550),
            child: ListView.builder(
              itemCount: bookData.length,
              itemBuilder: (context, index) {
                return Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Container(
                          width: ScreenUtil().setWidth(200),
                          height: ScreenUtil().setHeight(125),
                          child: Image.network(bookData[index]["imageUrl"]),
                        ),
                        SizedBox(width: 25),
                        Container(
                          height: ScreenUtil().setHeight(125),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Text(
                                "${bookData[index]["title"]}",
                                style: TextStyle(
                                  fontSize: 18,
                                ),
                              ),
                              SizedBox(
                                height: 15,
                              ),
                              Text(
                                "${bookData[index]["time"]}",
                                // "${bookList[index]["name"]}",
                                style: TextStyle(
                                    color: Color.fromRGBO(179, 179, 179, 1),
                                    fontSize: 12),
                              )
                            ],
                          ),
                        ),
                        Container(
                          height: 20,
                          width: 60,
                          margin: EdgeInsets.fromLTRB(75, 0, 0, 0),
                          child: RaisedButton(
                            padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                            textColor: Colors.white,
                            child: Text(
                              "进入",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 12,
                              ),
                            ),
                            onPressed: () {
                              //路由跳转
                              Navigator.of(context).push(MaterialPageRoute(
                                  builder: (context) => BookPage()));
                            },
                            color: Colors.blue,
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(15)),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: 18)
                  ],
                );
              },
            ),
          ),
          // SizedBox(height: 20),
        ],
      ),
    );
  }

  Widget _swiperBuilder(BuildContext context, int index) {
    return (imageList[index]);
  }
}
